package lwh.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import lwh.utils.DBOpenHelper;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class CheckOutService {
	private DBOpenHelper dbOpenHelper;
	public CheckOutService(Context context) {
		this.dbOpenHelper = new DBOpenHelper(context);
	}
	public List<HashMap<String,Object>> getInitList(String subitemId){
		List<HashMap<String,Object>> data =new ArrayList<HashMap<String,Object>>();
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("SELECT	a.prj_unit_range_id,a.prj_unit_batch_id,b.prj_unit_range_name,c.partition_name,a.prj_unit_batch_name,a.prj_unit_batch_desc,d.prj_progress_percent,d.prj_progress_id from prj_exm_unit_batch_model_detail a,prj_contract_unit_range b,exm_partition c,prj_progress d where a.partition_id=? and a.prj_unit_range_id=b.prj_unit_range_id and c.partition_id=a.partition_id AND a.prj_unit_batch_id=d.prj_unit_batch_id",
				new String[]{subitemId});
		while(cursor.moveToNext()){
			HashMap<String,Object> item=new HashMap<String, Object>();
    		item.put("prj_unit_range_id", cursor.getString(cursor.getColumnIndex("prj_unit_range_id")));
    		item.put("prj_unit_batch_id", cursor.getString(cursor.getColumnIndex("prj_unit_batch_id")));
    		item.put("prj_unit_range_name", cursor.getString(cursor.getColumnIndex("prj_unit_range_name")));
    		item.put("partition_name", cursor.getString(cursor.getColumnIndex("partition_name")));
    		item.put("prj_unit_batch_name", cursor.getString(cursor.getColumnIndex("prj_unit_batch_name")));
    		item.put("prj_unit_batch_desc", cursor.getString(cursor.getColumnIndex("prj_unit_batch_desc")));
    		item.put("progress", cursor.getString(cursor.getColumnIndex("prj_progress_percent")));
    		item.put("prj_progress_id", cursor.getString(cursor.getColumnIndex("prj_progress_id")));
    		data.add(item);
		}
		cursor.close();
		db.close();
		return data;
	}
	
	public List<HashMap<String,Object>> getListBySearchText(String subitemId,String SearchText){
		List<HashMap<String,Object>> data =new ArrayList<HashMap<String,Object>>();
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("SELECT	a.prj_unit_batch_id,b.prj_unit_range_name,c.partition_name,a.prj_unit_batch_name,a.prj_unit_batch_desc,d.prj_progress_percent,d.prj_progress_id from prj_exm_unit_batch_model_detail a,prj_contract_unit_range b,exm_partition c,prj_progress d where a.partition_id=? and a.prj_unit_range_id=b.prj_unit_range_id and c.partition_id=a.partition_id AND a.prj_unit_batch_id=d.prj_unit_batch_id and prj_unit_batch_name like ?",
				new String[]{subitemId,"%"+SearchText+"%"});
		while(cursor.moveToNext()){
			HashMap<String,Object> item=new HashMap<String, Object>();
			item.put("prj_unit_batch_id", cursor.getString(cursor.getColumnIndex("prj_unit_batch_id")));
    		item.put("prj_unit_range_name", cursor.getString(cursor.getColumnIndex("prj_unit_range_name")));
    		item.put("partition_name", cursor.getString(cursor.getColumnIndex("partition_name")));
    		item.put("prj_unit_batch_name", cursor.getString(cursor.getColumnIndex("prj_unit_batch_name")));
    		item.put("prj_unit_batch_desc", cursor.getString(cursor.getColumnIndex("prj_unit_batch_desc")));
    		item.put("progress", cursor.getString(cursor.getColumnIndex("prj_progress_percent")));
    		item.put("prj_progress_id", cursor.getString(cursor.getColumnIndex("prj_progress_id")));
    		data.add(item);
		}
		cursor.close();
		db.close();
		return data;
	}
	public void closeDBHelper(){
		dbOpenHelper.close();
	}
}
